﻿using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using Meotom.Upload.v2.Data;
using Meotom.Upload.v2.Data.BLL;

namespace Meotom.Upload.v2.Account
{
    public partial class FrmLostPassword : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LoginButton_OnClick(object sender, EventArgs e)
        {
            var userName = Membership.GetUserNameByEmail(txtEmail.Text);
            if (userName == null)
            {
                lblError.InnerHtml = "* Email không tồn tại !!!";
            }
            else
            {
                var user = Membership.GetUser(userName);
                var isLostPass = user != null && WProfile.GetProfile(user.UserName).UserInfo.IsLostPass;
                if (isLostPass)
                {
                    var profile = WProfile.GetProfile(user.UserName);
                    profile.UserInfo.IsLostPass = true;
                    profile.Save();

                    //var strPasswordTemp = user.ResetPassword();

                    using (var sReader = new System.IO.StreamReader(Configs.FolderSaveData + @"\template\LostPassword_Step1.htm"))
                    {
                        var strTemp = sReader.ReadToEnd();
                        strTemp = strTemp.Replace("{0}", Configs.HomeUrl);
                        strTemp = strTemp.Replace("{1}", Convert.ToString(user.ProviderUserKey));

                        var mailer = new mailer.MeoTom_Services();
                        mailer.SendMail(user.Email, "[Up.Meotom.Net] - Quen mat khau - Buoc 1", strTemp, "");
                    }

                    ScriptManager.RegisterStartupScript(this, GetType(), "key", "alert('Thông tin đã được gửi vào Email của bạn !!');",
                                                        true);
                }
                else
                {
                    Response.Redirect("frmLogin.aspx");
                }
            }
        }
    }
}